package com.chif.goingplus.service;

import com.chif.goingplus.dao.ClientMapper;
import com.chif.goingplus.pojo.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    /**
     * 创建订单时先查询会员是否存在 存在返回id 不存在创建会员
     * @param client
     * @return
     */
    @Override
    public int addClient(Client client) {
        //创建订单时先查询用户是否存在
        Client clientByName = clientMapper.queryClientByName(client.getClname());
        if (clientByName!=null){
            //存在
            return clientByName.getClid();
        }
        clientMapper.addClient(client);
        return client.getClid();
    }

    @Override
    public int deleteClientById(int id) {
        return clientMapper.deleteClientById(id);
    }

    @Override
    public int updateClient(Client bus) {
        return clientMapper.updateClient(bus);
    }

    @Override
    public Client queryClientById(int id) {
        return clientMapper.queryClientById(id);
    }

    @Override
    public List<Client> queryAllClient() {
        return clientMapper.queryAllClient();
    }

    @Override
    public Client queryClientByName(String name) {
        return clientMapper.queryClientByName(name);
    }

    /**
     * 新建用户时 会员名存在返回0   不存在新建会员
     * @param client
     * @return
     */
    @Override
    public int createClient(Client client) {
        //创建会员时先查询用户是否存在
        Client clientByName = clientMapper.queryClientByName(client.getClname());
        if (clientByName!=null){
            //存在
            return 0;
        }
        clientMapper.addClient(client);
        return client.getClid();
    }
}
